package com.juyu.miliao.api.service;

import com.juyu.miliao.api.dao.elasticsearch.FindDynamicGiftInfoElasticsearch;
import com.juyu.miliao.api.dao.mysql.FindDynamicGiftDao;
import com.juyu.miliao.api.dao.redis.usercache.UserCacheVo;
import com.juyu.miliao.api.dao.redis.usercache.UserInfoCache;
import com.juyu.miliao.api.domain.FindDynamicCommentInfo;
import com.juyu.miliao.api.domain.FindDynamicGiftInfo;
import com.juyu.miliao.api.domain.FindDynamicInfo;
import com.juyu.miliao.api.domain.Gift;
import com.juyu.miliao.api.domain.vo.FindDynamicGiftVO;
import com.juyu.miliao.api.domain.vo.FindDynamicVO;
import com.juyu.miliao.common.constant.SpendSystemMessage;
import com.juyu.miliao.common.constant.SpendSystemMessageType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author xuanqicheng
 */
@Service
public class FindDynamicGiftService {


    @Autowired
    private FindDynamicGiftDao findDynamicGiftDao;

    @Autowired
    private FindDynamicGiftInfoElasticsearch findDynamicGiftInfoElasticsearch;

    @Autowired
    private VestRobotMessageService vestRobotMessageService;

    @Autowired
    private UserInfoCache userInfoCache;




    /**
     * 添加
     * @param findDynamicGiftInfo
     */
    @Transactional(rollbackFor = Exception.class)
    public void addGiftSpend(FindDynamicInfo findDynamicInfo, Gift gift,
                             FindDynamicGiftInfo findDynamicGiftInfo) throws IOException {
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        findDynamicGiftInfo.setCreateDate(df.format(LocalDate.now()));
        DateTimeFormatter dfTime = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        findDynamicGiftInfo.setCreateTime(dfTime.format(LocalDateTime.now()));
        findDynamicGiftDao.addFindDynamicGiftSendInfo(findDynamicGiftInfo);
        findDynamicGiftInfoElasticsearch.addFindDynamicGiftSendInfo(findDynamicGiftInfo);

        UserCacheVo spendUser = userInfoCache.getUserCacheInfo(findDynamicGiftInfo.getSendUserId());
        sendSystemMessage(findDynamicInfo.getFindId(),findDynamicInfo.getUserId(),spendUser.getNickName(),
                findDynamicInfo.getFindTitle(),gift.getGiftName());
    }


    /**
     * 发送自定义评论区送礼物提醒消息
     * @param findId
     * @param userId 消息接受者
     */
    private void sendSystemMessage(int findId,long userId,String spendUserNickName,String findTitle,String giftName) {
        String type = SpendSystemMessageType.FIND_DYNAMIC_COMMENT_GIVE_GIFT_TYPE.getCode();
        Map<String,Object> ext = new HashMap<>(1);
        ext.put("findId",findId);

        String template = SpendSystemMessage.FIND_DYNAMIC_COMMENT_GIVE_GIFT.getMessage();
        String[] colors = SpendSystemMessage.FIND_DYNAMIC_COMMENT_GIVE_GIFT.getColors();
        ext.put("template",template);

        List<String> templateList = new ArrayList<>(1);

        templateList.add(spendUserNickName);
        templateList.add(findTitle);
        templateList.add(giftName);

        ext.put("templateValue",templateList);
        ext.put("templateColors",colors);

        String message = SpendSystemMessage.FIND_DYNAMIC_COMMENT_GIVE_GIFT
                .getMessage(spendUserNickName,findTitle,giftName);

        vestRobotMessageService.getSendMsg(userId,message,type,ext);
    }





}
